package org.ielse.frame.bluetooth;

import java.util.UUID;

import android.util.Log;

/**
 * @author QQ517309507
 */
public class BleController {

	// Service
	public static final UUID SERVICE = UUID.fromString("00000000-0000-1000-8000-00805F9B34FB");
	// Characteristic
	public final static UUID CHARACTERISTIC_CLIENT_NOTIFICATION = UUID.fromString("00002902-0000-1000-8000-00805f9b34fb");
	public static final UUID CHARACTERISTIC_ = UUID.fromString("00000000-0000-1000-8000-00805F9B34FB");
	// Value
	public static final byte[] VALUE_ = {};

	// Message
	public static final byte MESSAGE_ = 0x00;

	public BleController(OnMessageReceivedListener listener) {
		if (listener == null) throw new IllegalArgumentException("empty listener");
		this.listener = listener;
	}

	private OnMessageReceivedListener listener;

	public interface OnMessageReceivedListener {
		void logic();
	}

	public void dealRingMessage(byte[] message) {
		if (message == null || message.length < 1) {
			Log.e(getClass().getSimpleName(), "empty ble message");
			return;
		}
		switch (message[0]) {
		case 0:
			listener.logic();
			break;
		}
	}

	public static String bytesToHex(byte[] bytes) {
		if (bytes == null) {
			return "";
		}
		final char[] hexArray = "0123456789ABCDEF".toCharArray();
		char[] hexChars = new char[bytes.length * 2];
		for (int j = 0; j < bytes.length; j++) {
			int v = bytes[j] & 0xFF;
			hexChars[j * 2] = hexArray[v >>> 4];
			hexChars[j * 2 + 1] = hexArray[v & 0x0F];
		}
		return new String(hexChars);
	}

}
